package com.huawei.secure.android.common.ssl;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;

public class c {
    private static final String TAG = "FileUtil";
    public static final String v = "aegis";
    public static final String w = "backup.bks";
    public static final String x = "hmsrootcas.bks";

    public static boolean a(Context context, String str) throws e {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        if (context == null) {
            Log.e(TAG, "getBksPath: context is null");
            return false;
        }
        String a = a(context);
        c(context);
        return h.b(str, a);
    }

    public static int b(String str) {
        if (TextUtils.isEmpty(str)) {
            return -1;
        }
        File file = new File(str);
        if (file.exists()) {
            Log.w(TAG, "The directory  has already exists");
            return 1;
        } else if (file.mkdirs()) {
            Log.d(TAG, "create directory  success");
            return 0;
        } else {
            Log.e(TAG, "create directory  failed");
            return -1;
        }
    }

    public static String a(Context context) {
        if (context == null) {
            return null;
        }
        String str = context.getFilesDir().getAbsolutePath() + File.separator + v;
        b(str);
        return str;
    }

    public static String b(Context context) {
        if (context == null) {
            return null;
        }
        String a = a(context);
        File file = new File(a);
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            if (a == null) {
                return null;
            }
            for (File file2 : listFiles) {
                if (x.equals(file2.getName())) {
                    return file2.getAbsolutePath();
                }
            }
        }
        file = new File(a, w);
        if (file.exists()) {
            return file.getAbsolutePath();
        }
        return null;
    }

    public static void c(Context context) {
        if (context != null) {
            Object b = b(context);
            if (TextUtils.isEmpty(b)) {
                Log.e(TAG, "changeOldBksName: bks is null");
                return;
            }
            Log.i(TAG, "changeOldBksName: begin");
            String a = a(context);
            File file = new File(b);
            if (x.equals(file.getName())) {
                d(context);
            }
            file.renameTo(new File(a, w));
        }
    }

    public static void d(Context context) {
        File file = new File(a(context), w);
        if (file.exists()) {
            Log.e(TAG, "deleteOldBackup: ");
            file.delete();
        }
    }
}
